package Controller;

import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JLabel;

import Model.Person;
/**
 * Listener de tous les élements grqphiques cliquables
 * @author antoinerichard
 *
 */
public class ClickListener implements ActionListener, WindowListener,
		MouseListener {

	AdressBookController controller;
	static ArrayList<String> currentlyModified;

	public ClickListener(AdressBookController a) {
		this.controller = a;
	}

	public ClickListener() {

	}
/**
 * Selection de l'action en fonction de la source
 */
	@Override
	public void actionPerformed(ActionEvent e) {

		controller.Cadre.getList().clearSelection();

		

		if (e.getSource() == controller.Cadre.getAddButton()) {

			controller.Cadre.switchToAddView();
			controller.Cadre.setModifyMode(0);
		}

		if (e.getSource() == controller.Cadre.getModifyButton()) {
			controller.Cadre.switchToModifyView();
			controller.Cadre.setModifyMode(1);
			this.currentlyModified = controller.Cadre.getContactData();
			
			
		}
		if (e.getSource() == controller.Cadre.getDeleteButton()) {
			
			
		
			

				if(!controller.Livre.deleteContact(controller.Cadre.getDateLbl().getText())) controller.Cadre.popup("Pas de contact selectionné");
				controller.Cadre.selectFirstContact();
				controller.Cadre.reloadList(controller.Livre.getContactList());

		}
		if (e.getSource() == controller.Cadre.getSaveButton()) {

			ArrayList<String> dataset = controller.Cadre.getContactData();

			if (controller.Cadre.getModifyMode() == 0) {
				controller.Livre.createContact(dataset.get(0), dataset.get(1),
						dataset.get(6), dataset.get(4), dataset.get(5),
						dataset.get(3), dataset.get(2));
			} else {

				controller.Livre.updateContact(this.currentlyModified, dataset);
			}

			controller.Cadre.reloadList(controller.Livre.getContactList());
		}
		
		
		if(e.getSource() == controller.Cadre.getChooseBtn())
		{
			final JFileChooser fc = new JFileChooser();
			//In response to a button click:
			int returnVal = fc.showOpenDialog(controller.Cadre);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
	            File file = fc.getSelectedFile();
	            //This is where a real application would open the file.
	            System.out.println("Opening: " + file.getName());
	            try {
	            	
					Files.copy(file.toPath(),Paths.get("Resources/"+file.getName()));
					 File newFile = new File("Resources/"+file.getName());
					 newFile.renameTo(new File("Resources/"+Person.fileDate(controller.Livre.findPersonByData(controller.Cadre.getDateLbl().getText()).getDate())+".png"));
					 
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
	        }
		}

	}

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosing(WindowEvent e) {
		System.out.println("exit");
		controller.Livre.saveContactsInFile();
		System.exit(0);

	}

	@Override
	public void windowClosed(WindowEvent e) {

	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseClicked(MouseEvent e) {

		//lancement du navigateur
		if (e.getSource() == controller.Cadre.getWebsiteLbl()) {
			Desktop desktop = null;
			java.net.URI url;
			try {
				JLabel temp = (JLabel) e.getSource();
				String adresse = "http://" + temp.getText();

				url = new java.net.URI(adresse);
				if (Desktop.isDesktopSupported()) {
					desktop = Desktop.getDesktop();
					desktop.browse(url);
				}
			} catch (Exception ex) {
				System.out.println(ex.getMessage());
			}
		}

		
		//Lancement du client mail
		if (e.getSource() == controller.Cadre.getMailLbl()) {
			Desktop desktop = null;
			java.net.URI url;
			try {
				JLabel temp = (JLabel) e.getSource();
				String adresse = "mailto:" + temp.getText();

				url = new java.net.URI(adresse);
				if (Desktop.isDesktopSupported()) {
					desktop = Desktop.getDesktop();
					desktop.mail(url);
				}
			} catch (Exception ex) {
				System.out.println(ex.getMessage());
			}
		}

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

}
